package com.edu.controller;

import com.edu.bean.Cart;
import com.edu.bean.Orders;
import com.edu.common.R;
import com.edu.service.CartService;
import com.edu.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@ResponseBody
@CrossOrigin
@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/createOrders")
    public R createOrders(Orders orders) {
        try {
            int orderId = ordersService.createOrders(orders);

            return R.ok().data(orderId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/getOrdersById/{orderId}")
    public R getOrdersById(@PathVariable("orderId") Integer orders) {
        try {
            Orders orders1 = ordersService.getOrdersById(orders);
            return R.ok().data(orders1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/isTruePayment/{orderId}")
    public R isTruePayment(@PathVariable("orderId") Integer orderId) {
        try {
            int i = ordersService.isTruePayment(orderId);
            return R.ok().data(1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @GetMapping("/listOrdersByUserId/{userId}")
    public R listOrdersByUserId(@PathVariable("userId") String userId)
    {
        try {
            List<Orders> orders = ordersService.listOrdersByUserId(userId);
            return R.ok().data(orders);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
